let axios = require('axios')

// 用promise异步等待，防止对方服务器瘫痪

//设置innertmanyPromise对象,插入许多数据函数
function insertMany(coll, arr) {
    return new Promise((resolve, reject) => {
        let mongodb = require('mongodb').MongoClient;
        let url = "mongodb://localhost:27017";
        mongodb.connect(url, {
            useNewUrlParser: true,
            useUnifiedTopology: true
        }, function (err, db) {
            if (err) {
                throw err;
            }
            var dbb = db.db('local');
            dbb.collection(coll).insertMany(arr, function (err, res) {
                if (err) throw err;
                // console.log('操作成功');
                db.close();
                resolve();
            })
        })
    })
}

//获取英雄列表
async function getHeroList() {
    let httpUrl= 'https://pvp.qq.com/web201605/js/herolist.json';
    let result = await axios.get(httpUrl);
    console.log('英雄有了');
    await insertMany('王者荣耀', result.data);
    return result.data;
}

getHeroList();